<?php
/**
 * Created by PhpStorm.
 * User: windon
 * Date: 2018/6/3
 * Time: 14:53
 */

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class FriendUser extends Model
{
    protected $table = 'friend_user';

    public function friendUser(){
        return $this->hasOne('App\Models\User', 'id', 'friend_id');
    }
    public function user(){
        return $this->hasOne('App\Models\User', 'id', 'uid');
    }

    /*
     * 获取单行
     * */
    public static function getRowsObj($where=[], $feild = '*')
    {
        return self::select($feild)->where($where)->first();
    }

    /*
     * 是否亲友
     * */
    public static function is_attention($uid, $friend_uid)
    {
        if(!empty($uid) && !empty($friend_uid)){
            $key = 'user:is_attention:u_'.$uid.'_f_'.$friend_uid;
            if(!empty(Redis::get($key)))
                return true;

            $rows = self::select('id')->where([['uid', '=', $uid], ['friend_id', '=', $friend_uid], ['status', '=', '1'], ['type', '=', 3]])->first();
            if(!empty($rows)){
                $expires_in = rand(3600, 3600*24);
                Redis::setex($key, $expires_in, 1);
                return true;
            }
        }
        return false;
    }

}